GoLang

Explain How to sort a slice of ints in GoLang? | Go Programming Tutorial

Explain How to sort a slice of ints in GoLang? | Go Programming Tutorial

Explain How to sort a slice of ints in GoLang? | Go Programming Tutorial

In this article, you will learn how to sort the elements of the slice data type in the Go Programming Language.

A slice is a kind of data type which is similar to an array which is used to hold the items of the same data type. The only difference between array and slice data types is that slice can vary in size dynamically but it is not feasible in arrays. 

Go Language permits you to sort the elements of the slice according to its type. So, the int-type slice is sorted with help of the following functions. These functions are mentioned under the sort package so for using the sort packages you have to import sort packages in the program:

1. Ints: This function helps in only sorting a slice of ints and it sorts the items of the slice in increasing order.

Syntax:

func Ints(slc [ ]int)

Here, slc represents a slice of ints. 

Example:

package main
import (
    "fmt"
    "sort"
)
   
// Main function
func main() {
       
    // Create and initialize slices
    // Using shorthand declaration
    slice_1 := []int{40, 608, 10, 300, 800, 100, 900}
    slice_2 := []int{-34, 507, -409, 67, 10, 12, -5}
       
    // Displaying slices
    fmt.Println("Slices(Before):")
    fmt.Println("Slice 1: ", slice_1)
    fmt.Println("Slice 2: ", slice_2)
       
    // Sorting the slice of intsUsing Ints function
    sort.Ints (slice_1)
    sort.Ints (slice_2)
       
    // Displaying the result
    fmt.Println("\nSlices(After):")
    fmt.Println("Slice 1 : ", slice_1)
    fmt.Println("Slice 2 : ",slice_2)
}

Output:

Slices(Before)

Slice 1: [40, 608, 10, 300, 800, 100, 900]

Slice 2:  [-37, 509, -409, 67, 10, 12, -5]

Slices(After)

Slice 1: [10, 40, 100, 300, 608, 800,900]

Slice 2: [-409, -37, -5, 10, 12, 57, 509]

2. IntsAreSorted: This function helps in checking whether the given slice of ints is in sorted form or not. Here, the sorted form means increasing order. The method returns true if the slice is in increasing order, or returns false if the slice is not in increasing order.

Syntax:

func IntsAreSorted( scl [ ]int) bool

Example:

package main
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and initializing slices
    // Using shorthand declaration
    slice_1 := []int{40, 10, 300, 400, 100, 6100, 700}
    slice_2 := []int{-35, 567, -407, 67, 40, 12, -5}
  
    // Displaying slices
    fmt.Println("Slices:")
    fmt.Println("Slice 1: ", slice_1)
    fmt.Println("Slice 2: ", slice_2)
  
    // Checking the slice is in sorted form or not
    // Using IntsAreSorted function
    res_1 := sort.IntsAreSorted(slice_1)
    res_2 := sort.IntsAreSorted(slice_2)
  
    // Displaying the result
    fmt.Println("\nResult:")
    fmt.Println("Is Slice 1 is sorted?: ", res_1)
    fmt.Println("Is Slice 2 is sorted?: ", res_2)
}
 
Output:
Slices:
Slice 1: [40, 10, 300, 400, 100, 6100, 700]
Slice 2: [-35, 567, -407, 67, 40, 12, -35]
 
Result:
Is Slice 1 is sorted?: False
Is Slice 2 is sorted?: False
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.